import { query,add,remove,edit } from '@/services/daily_analysis_report';
import moment from 'moment'
export default {
  namespace: 'daily_analysis_report',
  state: {
    pageLoaded:false,
    tableY:300,
    data:[],
    scrollData:{x:0,y:0},
    selectedRowKeys: [0],
    searchParams:{
      sampling_location:'塔尔浪水源水',
      month:moment().format('YYYY-MM'),
      page: 1,
      per_page: 30,
    },
    meta: {total: 0, per_page: 30,current_page:1},
    additional: {total_count: 0, online_count: 0},
    loading: true,
    editRecord:{},
    detail:{
      isDetail:false,
    }

  },
  effects: {
    *changeIsDetail({ payload, callback }, { call, put ,select}) {
      console.log('changeIsDetail')
      const {detail} = yield select(state => state.daily_analysis_report)
      yield put({
        type: 'changeDetail',
        payload:{
          ...detail,
          isDetail:!detail.isDetail,
          detailId:payload.id,
        }
      });
    },
    *reset({ payload, callback }, { call, put }) {
      console.log('reset=============')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeTableY({ payload, callback }, { call, put }) {
      const tableY=document.body.offsetHeight -
        document.querySelector('.react-grid-Container').getBoundingClientRect().top -10;
        yield put({
          type: 'saveTableY',
          payload: tableY
        });
    },
    *changeSearchParams({ payload, callback }, { call, put }) {
     console.log('changeSearchParams')
      yield put({
        type: 'saveSearchParams',
        payload: payload
      });
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.daily_analysis_report)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.daily_analysis_report)//获取state中的数据
      payload=payload||state.searchParams;
      console.log('payload',payload)
      const response = yield call(query, payload);
      console.log(response)
      if(response.status===200){
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'save',
          payload:  response.data
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }

    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(add, payload);

      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }else{
      }
    },
    *edit({ payload, callback }, { call, put }) {
      console.log('edit')
      const response = yield call(edit, payload);
      console.log(response)
      if(response.status===200){
        if (callback) callback();
      }
    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload,
      });
    },
    *remove({ payload, callback }, { call, put }) {
      const response = yield call(remove, payload);
      if(response.status===200){
        if (callback) callback();
      }
    },
  },

  reducers: {
    save(state, action) {
      const sampling_location=state.searchParams.sampling_location;
      console.log('sampling_location',sampling_location)
      let hzd='≤1.0';
      let ad='≤0.8';
      let sd='≤15';
      if(sampling_location==='示范区水厂出厂水'||sampling_location==='七泉湖出厂水'||sampling_location==='七泉湖进场水'){
        ad='0.30-4.0'
      }else if(sampling_location==='塔尔浪出厂水'||sampling_location==='老城区水厂出厂水'){
        ad='0.10-0.80'
      }
      if(sampling_location==='塔尔浪水源水'){
        ad='≤1';
        sd='-';
        hzd='--'
      }
      if(sampling_location==='七泉湖进场水'){
        hzd='--'
      }
      if(sampling_location==='示范区水厂出厂水'){
        ad='0.30-2.0'
      }
      if(sampling_location==='七泉湖出厂水'){
        ad='0.30-2.0'
      }
      if(sampling_location==='七泉湖进场水'){
        ad='≤1'
      }
      action.payload.data.push({
        sampling_date:'限值',
        items:[
          {key:'hzd',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':hzd},
          {key:'sd',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':sd},
          {key:'rykjw',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':'无'},
          {key:'chw',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':'无异臭、异味'},
          {key:'ph',value:'6-9'},
          {key:'xjzs',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':'≤100'},
          {key:'zdcjq',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':'不得检出'},
          {key:'fdcjq',value:(sampling_location==='塔尔浪水源水'||sampling_location==='七泉湖进场水')?'--':'不得检出'},
          {key:'hyl',value:'≤6'},
          {key:'ad',value:ad},
        ]
      })
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveTableY(state, action) {
      return {
        ...state,
        tableY: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: {
          ...state.searchParams,
          ...action.payload
        },
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        tableY:state.tableY,
        data:[],
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchParams:{
          sampling_location:'塔尔郎水源水',
          month:moment().format('YYYY-MM'),
          page: 1,
          per_page: 30,
        },
        meta: {total: 0, per_page: 0,current_page:1},
        loading: true,
        editRecord:{},
        additional: {total_count: 0, online_count: 0},
        detail:{
          isDetail:false,
        }

      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
  },

};
